import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:tot_android_flutter/utils/utils_ext.dart';
import 'package:tot_android_flutter/utils/asset_ext.dart';

import '../../../themes/color_palettes.dart';
import 'LoadingStatus.dart';
import 'package:lottie/lottie.dart';

class LoadingStatusWidget extends StatelessWidget {
  final LoadingStatus loadingState;
  final Widget Function() contentWidget;
  final Function? retryCallback;

  LoadingStatusWidget({
    required this.loadingState,
    required this.contentWidget,
    this.retryCallback,
  });

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        switch (loadingState) {
          case LoadingStatus.loading:
            return _loadingStatusWidget();
          case LoadingStatus.content:
            return contentWidget(); // 显示内容
          case LoadingStatus.empty:
            return _emptyStatusWidget();
          case LoadingStatus.disconnect:
            return _disconnectStatusWidget();
          case LoadingStatus.error:
            return _errorStatusWidget();
        }
      },
    );
  }



// 加载中
  Widget _loadingStatusWidget() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          SizedBox(height: 40.w),
          ColorFiltered(
              colorFilter: ColorFilter.mode(
                ColorPalettes.instance.secondary,
                BlendMode.srcIn,
              ),
              child: Lottie.asset("view_loading".lottie,
                  width: 256.w, height: 256.w)),
          Text("加载中...",
              style: TextStyle(
                  color: ColorPalettes.instance.thirdText, fontSize: 18.w)),
          SizedBox(height: 40.w),
        ],
      ),
    );
  }

// 数据为空
  Widget _emptyStatusWidget() {
    return InkWell(
      onTap: () {
        retryCallback?.call();
      },
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(height: 40.w),
            Container(
                width: 256.w,
                height: 256.w,
                alignment: Alignment.center,
                child: Image.asset("ic_view_empty".webp,
                    width: 120.w,
                    height: 120.w,
                    color: ColorPalettes.instance.secondary)),
            Text("暂无数据，点击刷新",
                style: TextStyle(
                    color: ColorPalettes.instance.thirdText, fontSize: 18.w)),
            SizedBox(height: 40.w),
          ],
        ),
      ),
    );
  }

// 网络不可用
  Widget _disconnectStatusWidget() {
    return InkWell(
      onTap: () {
        retryCallback?.call();
      },
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(height: 40.w),
            Container(
                width: 256.w,
                height: 256.w,
                alignment: Alignment.center,
                child: Image.asset(
                    "ic_view_network_error".webp,
                    width: 180.w,
                    height: 180.w,
                    color: ColorPalettes.instance.secondary)),

            Text("网络不可用",
                style: TextStyle(
                    color: ColorPalettes.instance.thirdText, fontSize: 18.w)),
            SizedBox(height: 40.w),
          ],
        ),
      ),
    );
  }

// 加载失败
  Widget _errorStatusWidget() {
    return InkWell(
      onTap: () {
        retryCallback?.call();
      },
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(height: 40.w),
            Container(
                width: 256.w,
                height: 256.w,
                alignment: Alignment.center,
                child: Image.asset(
                    "ic_view_fail".webp,
                    width: 180.w,
                    height: 180.w,
                    color: ColorPalettes.instance.secondary)),
            // Text("请求失败[${errorCode ?? RequestException.CODE_UNKOWN_ERROR}:${errorMessage ?? "未知错误"}]，点击重试",
            Text(" 未知错误，点击重试",
                style: TextStyle(
                    color: ColorPalettes.instance.thirdText, fontSize: 18.w)),
            SizedBox(height: 40.w),
          ],
        ),
      ),
    );
  }

}

